<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title></title>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"/>
    <style type="text/css" scoped="scoped">
        input {
            width: 200px;
            height: 30px;
        }
    </style>
</head>
<body>
<div id="app">
    <h1>上传作业</h1>
    <div style="margin: 50px;">
        <div class="clearfloat" style="float: left;">
            <el-button type="primary" @click="dialogFormVisible = true">添加作业</el-button>
        </div>

        <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item>
                <el-input v-model="workName" placeholder="可输入作业名字"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="onSubmit">搜索</el-button>
            </el-form-item>
        </el-form>
        <div style="text-align: center;">
        <#--el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
                              @selection-change="handleSelectionChange">-->
            <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%">
                <el-table-column prop="id" label="作业序号" width="150"></el-table-column>
                <el-table-column prop="name" label="作业名称" width="150"></el-table-column>
                <el-table-column prop="course.courseName" label="课程名称" width="150"></el-table-column>
                <el-table-column prop="student.studentName" label="学生姓名" width="150"></el-table-column>
                <el-table-column prop="teacher.teacherRealname" label="老师姓名" width="150"></el-table-column>
                <el-table-column prop="createDate" label="上传时间" :formatter="dateFormat"
                                 width="240"></el-table-column>

                <el-table-column label="操作">
                    <template slot-scope="scope">

                        <el-button class="el-icon-delete" size="mini" type="danger"
                                   @click="handleDelete(scope.row.id)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!--分页-->
        <div class="block">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                           :current-page="pageIndex" :page-size="pageSize" layout="total, prev, pager, next, jumper"
                           :total="total">
            </el-pagination>
        </div>
        <el-dialog title="上传作业" :visible.sync="dialogFormVisible">
            <el-form :model="addForm" label-width="180px" ref="addForm">

                <el-form-item label="作业名称：">
                    <el-input v-model="addForm.name"></el-input>
                </el-form-item>

                <el-upload
                        class="upload-demo"
                        action="/api/teacher/courseware/upload"
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :before-remove="beforeRemove"
                        multiple
                        :limit="1"
                        :on-exceed="handleExceed"
                        :on-success="uploadSuccess"
                        :on-error="uploadError"
                        :file-list="fileList">
                    <el-button size="small" type="primary">点击上传</el-button>
                </el-upload>
            </el-form>
            <span slot="footer" class="dialog-footer">
   				 <el-button @click="dialogFormVisible=false">取 消</el-button>
   					 <el-button type="primary" @click="addCourseware">确 定</el-button>
 			 </span>
        </el-dialog>

    </div>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
    let _this = new Vue({
        el: '#app',
        data: {
            workName:null,
            dialogFormVisible: false,
            addForm: {
                filePath:null,
                name:null

            },
            pageIndex: 1,
            pageSize: 5,
            total: 0,
            formInline: {
                coursewareName: ''
            },
            tableData: [],
            fileList: []
        },
        created: function () {
            this.coursewareList1();

        },
        methods: {


            /*日期格式化*/
            dateFormat: function (row, column) {
                let val = row[column.property];
                //console.log(date);
                let date = new Date(val);
                return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
            },
            uploadSuccess(response, file, fileList) {

                // this.addForm.coursewareSize = file.size;
                this.addForm.filePath = response.resData
            },
            // 上传错误
            uploadError(response, file, fileList) {
                console.log('上传失败，请重试！')
            },

            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            //点击已上传的文件链接时的钩子, 可以通过 file.response 拿到服务端返回数据
            handlePreview(file) {
                console.log('文件', file);
            },
            handleExceed(files, fileList) {
                this.$message.warning('当前限制选择 1个文件，本次选择了' + files.length + '个文件，共选择了' + (files.length + fileList.length) + '个文件');
            },
            beforeRemove(file, fileList) {
                return this.$confirm('确定移除' + file.name + '?');
            },

            /*分页*/
            handleSizeChange(val) {
            },
            handleCurrentChange(val) {
                this.pageIndex = val;
                this.onSubmit();
            },
            /*显示课件列表*/
            coursewareList1() {
                $.ajax({
                    type: 'post',
                    url: '/api/student/studentWork',
                    data: {pageIndex: this.pageIndex,name:this.workName},
                    dataType: 'json',
                    success: function (res) {
                        console.log(res)
                        _this.tableData = res.resData.list;
                        _this.total = res.resData.total
                    }
                })
            },

            /*查询课件
             文件类型查询未实现
             * */
            onSubmit() {
                this.coursewareList1();
            },

            /*根据id删除课件
             需要修改返回message
             * */
            handleDelete(id) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    type: 'warning',
                    center: true
                }).then(function () {
                    $.ajax({
                        type: 'post',
                        url: '/api/student/workDelete',
                        data: {id: id},
                        dataType: 'json',
                        success: function (res) {
                            console.log(res)
                            if (res.status == 200) {
                                _this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                                _this.coursewareList1();
                            } else {
                                _this.$message({
                                    message: '删除失败',
                                    type: 'info'
                                })
                            }
                        }
                    })


                }).catch(function () {
                    _this.$message({
                        message: '取消删除',
                        type: 'info'
                    })
                })


            },
            /*添加课件*/
            addCourseware() {
                // console.log(this.addForm)

                $.ajax({
                    type: 'post',
                    url: '/api/student/workInsert',
                    contentType : 'application/json',
                    data: JSON.stringify(this.addForm),
                    dataType: 'json',
                    success: function (res) {
                        if (res.status == 200) {
                            _this.$message({
                                message: '添加成功',
                                type: 'success'
                            });
                            _this.addForm.name = '';
                            _this.addForm.filePath = '';
                            _this.dialogFormVisible = false;
                            _this.coursewareList1()
                        } else {
                            this.$message({
                                message: '添加失败',
                                type: 'info'
                            })
                        }
                    },
                    error : function(error) {
                        console.log('接口不通' + error);
                    }
                })

            }
        }
    })


</script>

</body>
</html>